function Validator(selector){
    if(selector){
        selector = selector+" *[data-v]";
    }else{
        selector = "*[data-v]";
    }
    Validator.prototype.validator_selector = selector;
    detectionLayer();
    var validator = this;
    $(selector).each(function(){
        var that = this;
        var tagType = $(that).data("v-type") || "input";
        if(tagType.toUpperCase() == "INPUT"){
            $(that).on("blur",function(){ //失去焦点
                validator.validation($(this));
            }).on("focus",function(){//焦点聚交
                var tagType = $(this).data("v-type")||"input";
                switch(tagType.toUpperCase()){
                    case "INPUT":
                        $(this).removeClass("v-error");
                        break;
                }
            })
        }
    })
}
function detectionLayer(){
    try{ layer }catch(e){
        $("body").append("<script type='text/javascript' src='/plugin/layer-3.0.3/layer.js'></script>")
    }
}
Validator.prototype.initCss = function(){
     var css = '.v-error{ border-color:red; }';
     $("head").append("<style type='text/css'>"+css+"</style>")
}
//验证所有，在提交时进行调用
Validator.prototype.validate = function(){
    var validator = this;
    $(this.validator_selector).each(function(){
        validator.validation($(this));
    })
};
Validator.prototype.verify = function(value,regex){
    var result = false,pattern;
    if(value && regex){
        if(typeof regex == "string"){
            pattern = new RegExp(regex,"g");
        }else{
            pattern = regex;
        }
        result = pattern.test(value);
    }
    return result;
};
Validator.prototype.getRegexPattern = function(validateType,customRegex){
    var regex = /\S/g;
    switch (validateType){
        case "number":
           regex = /^[0-9]{1,20}$/g;
           break;
        case "phone":
            regex = /^[1-9]{11}/g;
            break;
        case "custom":
            regex = customRegex;
            break;
    }

    return regex;
};
Validator.prototype.getValue = function(tagType,targetEle){
    var value = '';
    switch (tagType.toUpperCase()){
        case "INPUT":
            value = targetEle.val();
            break;
        case "SELECT":
            value = targetEle.val();
            break;
        case "OTHER":
            var valueEval = targetEle.data("v-value");
            value = eval(valueEval);
            break;
    }
    return value;
};
Validator.prototype.message = function(msgType,targetTag){

    switch (msgType){
        case "border":
            targetTag.addClass("v-error");
            break;
        case "alert":
            var msg = targetTag.data("v-msg");
            layer.msg(msg||"请输入相关信息!");
            break;
    }
};
Validator.prototype.stop = function(){
    throw SyntaxError("stop execute!");
};

Validator.prototype.validation = function(targetEle){
    var validator = this;
    var tagType = targetEle.data("v-type") || "input";
    var msgType = targetEle.data("v-msg-type")||"border";
    var customRegex = targetEle.data("v-regex");
    var validationType = targetEle.data("v-validate-type");
    var value = validator.getValue(tagType,targetEle) || '';
    var regex = validator.getRegexPattern(validationType,customRegex);
    var result = validator.verify(value,regex);
    if(!result){
        validator.message(msgType,targetEle);
        validator.stop();
    }
};